import scrapy
from scrapy import Request
import os

from goodDesignImage2018Spider.items import GooddesignimagespiderItem

base_url_2020 = 'https://www.g-mark.org/award/search/standard?year=2020&lastAwardNo='
base_url_2019 = 'https://www.g-mark.org/award/search/standard?year=2019&lastAwardNo='
base_url_2018 = 'https://www.g-mark.org/award/search/standard?year=2018&lastAwardNo='
base_url_2017 = 'https://www.g-mark.org/award/search/standard?year=2017&lastAwardNo='
base_url_2016 = 'https://www.g-mark.org/award/search/standard?year=2016&lastAwardNo='
base_url = 'https://www.g-mark.org'

search_url_2017 = "https://www.g-mark.org/award/search?from=2017&to=2017&prizeCode=&keyword="
search_url_2016 = "https://www.g-mark.org/award/search?from=2016&to=2016&prizeCode=&keyword="

# 文件跟路径
file_base_path = "F:/goodDesign/2017/"


# 处理goodDesign首页默认的那些奖项图片：第一个到包括longLife结束
class Gooddesignimg2017spiderSpider2(scrapy.Spider):
    name = 'goodDesignImg2017Spider2'
    allowed_domains = ['g-mark.org']
    start_urls = ['https://www.g-mark.org/']

    def start_requests(self):
        yield Request(url=search_url_2016, callback=self.parse)

    def parse(self, response):
        # 该年份下特殊奖项的所有section
        all_sections = response.xpath("/html/body/div[3]/div/div[2]/section/section")
        for item in all_sections:
            # 獎項名稱
            award_type = item.xpath("./h3/img/@alt")[0].extract()
            # section下所有奖项li
            section_lis = item.xpath("./ul/li")
            for item_li in section_lis:
                # 详情地址
                detail_url = base_url + item_li.xpath("./a[1]/@href")[0].extract()
                # award_id
                award_id = item_li.xpath("./@id")[0].extract()
                # award_code
                award_code_path = item_li.xpath('./a[1]/figure/img/@data-original')[0].extract()
                # /分割字符串
                path_list = award_code_path.split('/')
                # 获取award_code
                award_code = path_list[-2]

                # 构建实体
                img_item = GooddesignimagespiderItem()
                # 获奖id
                img_item['award_id'] = award_id
                # 图片号
                img_item['award_code'] = award_code
                # 获奖产品详情页地址
                img_item['detail_url'] = detail_url
                img_item['year'] = '2016'
                # 下载状态 0未下载  1下载成功 -1下载失败
                img_item['download_status'] = 0
                img_item['award_type'] = award_type
                yield Request(url=detail_url, callback=self.parse_detail, meta={"img_item": img_item})

    def parse_detail(self, response):
        img_item = response.meta["img_item"]
        print(img_item['award_id'])
        img_li = response.xpath('/html/body/div[3]/div/section/section/figure/ul[1]/li')
        for img in img_li:
            img_src = img.xpath('./a/img/@src')[0].extract()
            print(img_src)
            # 图片下载地址
            img_item['img_download_url'] = img_src
            # 获取文件名
            file_name = os.path.basename(img_src)
            file_name = file_name[0:file_name.rfind('?', 1)]
            img_item['img_file_name'] = file_name
            yield img_item
